from product_upload.util.basic.mysql_util import db_get_one


class BasicProduct:
    # 字段顺序与数据库字段一致
    fields = ['id', 'status', 'published', 'not_available', 'platform', 'spu', 'sku', 'class_name', 'product_type',
              'catalog_id', 'catalog_name', 'link', 'main_image', 'title', 'dimensions', 'packages', 'search_words',
              'price', 'quantity', 'remark', 'json_text', 'product_info', 'create_time', 'update_time', 'update_by',
              'supplier_code', 'supplier_name', 'region', 'ship_fee', "image_count", 'extra_field']

    def __init__(self, **kwargs):
        self.id = kwargs.get('id', None)
        self.status = kwargs.get('status', None)
        self.published = kwargs.get('published', None)
        self.not_available = kwargs.get('not_available', "")
        self.platform = kwargs.get('platform', None)
        self.spu = kwargs.get('spu', None)
        self.sku = kwargs.get('sku', None)
        self.class_name = kwargs.get('class_name', None)
        self.product_type = kwargs.get('product_type', None)
        self.catalog_id = kwargs.get('catalog_id', None)
        self.catalog_name = kwargs.get('catalog_name', None)
        self.link = kwargs.get('link', None)
        self.main_image = kwargs.get('main_image', None)
        self.title = kwargs.get('title', None)
        self.dimensions = kwargs.get('dimensions', None)
        self.packages = kwargs.get('packages', None)
        self.search_words = kwargs.get('search_words', None)
        self.price = kwargs.get('price', 0)
        self.quantity = kwargs.get('quantity') if kwargs.get('quantity') else 0
        self.remark = kwargs.get('remark', None)
        self.json_text = kwargs.get('json_text', None)
        self.product_info = kwargs.get('product_info', None)
        self.create_time = kwargs.get('create_time', None)
        self.update_time = kwargs.get('update_time', None)
        self.update_by = kwargs.get('update_by', None)
        self.supplier_code = kwargs.get('supplier_code', None)
        self.supplier_name = kwargs.get('supplier_name', None)
        self.region = kwargs.get('region', None)
        self.ship_fee = kwargs.get('ship_fee', 0)
        self.image_count = kwargs.get('image_count', None)
        self.extra_field = kwargs.get('extra_field', None)

    @classmethod
    def from_db(cls, field_names, row) -> 'BasicProduct':
        if len(field_names) != len(row):
            raise ValueError("bug!---db field len != query field len")
        data_dict = dict(zip(field_names, row))
        return cls(**data_dict)

    def __repr__(self):
        field_info = []
        for field in self.fields:
            value = getattr(self, field)
            field_info.append(f"{field}={repr(value)}")
        return f"BasicProduct=[{', '.join(field_info)}]"


if __name__ == '__main__':
    print(db_get_one("basic_product", "id=3", BasicProduct))
    pass
